import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.utils.ByteArray;
/*
 The Blob class (BLitter OBject) is a rectangle of pixels that can be
  quickyly and easily copied to the screen.  Perhaps a better name would
  be Sprite, but that would be too confusing.
 The main purpose of this class is to do a "copyPixels", and it just gathers
  the required data together to make this very easy.
*/
class Blob
{
   var mArena:BitmapData;
   public var mBits:BitmapData;
   public var mRect:Rectangle;
   var mPoint:Point;
   var oldPixels: ByteArray;
   // The "hotspot" is the logical origin of the object, with respect
   //  to the top left of its bitmap rectangle.  This allows you to deal
   //  with the position of the object, without having to worry about drawing
   //  offsets etc.
   var mHotX:Float;
   var mHotY:Float;
   public var redraw:Bool;
 
// Passing the arena into the constructor is not really required,
   //  but doing this reduces the number of params we have to pass into
   //  the Draw function;
   public function new(inBits:BitmapData,inX:Int, inY:Int, inW:Int, inH:Int,
           ?inHotX:Null<Float>, ?inHotY:Null<Float>)
   {
      mBits = inBits;
      mRect = new Rectangle(inX,inY,inW,inH);
      mPoint = new Point(0,0);
      //oldPixels = new ByteArray();
      //oldPixels = mArena.getPixels(mRect);
      // If null is provided, assume the centre.
      mHotX = inHotX==null ? inW/2 : inHotX;
      mHotY = inHotY==null ? inH/2 : inHotY;
      redraw = false;
   }
   public function clear(inArena:BitmapData)
   {
      oldPixels.position = 0;
      var byteRect = new Rectangle(mPoint.x,mPoint.y,mRect.width,mRect.height);
      inArena.setPixels(byteRect,oldPixels);
   }
   public function draw(inArena:BitmapData,inX:Float,inY:Float) : BitmapData
   {
      oldPixels.position = 0;
      var byteRect:Rectangle;
      byteRect = new Rectangle(mPoint.x,mPoint.y,mRect.width,mRect.height);
//byteRect = new Rectangle(0,0,640,426);
       if(redraw)
	{
	    inArena.setPixels(byteRect,oldPixels);
	}
      redraw = false;
      mPoint.x = inX-mHotX;
      mPoint.y = inY-mHotY;
      byteRect = new Rectangle(mPoint.x,mPoint.y,mRect.width,mRect.height);
      oldPixels = inArena.getPixels(byteRect);
      inArena.copyPixels(mBits,mRect,mPoint,null,null,true);
      return inArena;
   }
} 
